package com.lin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.lin.model.Account;

public class AccountDao {
	public Account findByUserAndPwd(String usr, String pwd) {
		String sql = "select * from account where username=? and password=?";
		Connection conn = DBUtils.getDBConnection();
		
		Account account = null;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, usr);
			ps.setString(2, pwd);
			ResultSet rs = ps.executeQuery();
			while(rs!=null && rs.next()) {
				account = construct(rs);
			}
			ps.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return account;
	}
	
	private Account construct(ResultSet rs) throws Exception {
		Account account = null;
		if(rs!=null) {
			account = new Account();
			account.setId(rs.getInt("id"));
			account.setUserName(rs.getString("username"));
			account.setPassword(rs.getString("password"));
		}
		return account;
	}
}
